/**
 *====================================================
 * 文件名称: TallyingServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.project.service;

import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.common.util.BeanUtils;
import com.yuexunit.finance.common.util.RandomUtil;
import com.yuexunit.finance.project.SettingConstants;
import com.yuexunit.finance.project.entity.ContainerInventoryEntity;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.TallyingEntity;
import com.yuexunit.finance.project.entity.lexicon.FinanceStateEnum;
import com.yuexunit.finance.project.entity.lexicon.SupervisionStateEnum;
import com.yuexunit.finance.project.entity.lexicon.TallyingStateEnum;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.util.DateUtil;

/**
 * @ClassName: TallyingServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:56:17
 */
@Implements(TallyingService.class)
public class TallyingServiceImpl implements TallyingService {

	@Override
	public Long createTallying(TallyingEntity entity) {
		if (entity.getTallyingState() == null) {
			entity.setTallyingState(TallyingStateEnum.WAIT_TALLY);
		}
		entity.setTallyingNumber(SettingConstants.PRE_NUMBER_TALLYING + RandomUtil.generateTimeNumber());
		entity.setCreateDateTime(DateUtil.now());
		PersistUtil.insert(entity);
		return entity.getTallyingId();
	}

	@Override
	public void updateTallying(TallyingEntity entity) {
		TallyingEntity persist = inquireTallying(entity.getTallyingId());
		BeanUtils.copyProperties(entity, persist);
		PersistUtil.update(persist);
	}

	@Override
	public void renovateTallying(TallyingEntity persist) {
		PersistUtil.update(persist);
	}

	@Override
	public void deleteTallying(Long tallyingId) {
		PersistUtil.delete(TallyingEntity.class, tallyingId);
	}

	@Override
	public TallyingEntity inquireTallying(Long tallyingId) {
		return PersistUtil.get(TallyingEntity.class, tallyingId);
	}

	@Override
	public TallyingEntity inquireTallyingDetail(Long tallyingId) {
		TallyingEntity entity = PersistUtil.get(TallyingEntity.class, tallyingId);
		if (entity == null) {
			throw new BusinessException("理货任务不存在");
		}
		return entity;
	}

	@Override
	public PageList<TallyingEntity> inquireTallyingPageList(Long[] tallyingIds, Long financeId, Long recieveNoticeId, Long logisticsCompanyId, String tallyingNumber, Long clerkId, Integer[] tallyingState, Boolean sort, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TallyingEntity.class);
		if (tallyingIds != null && tallyingIds.length > 0) {
			criteria.add(Restrictions.in("tallyingId", tallyingIds));
		}
		if (financeId != null) {
			criteria.add(Restrictions.eq("financeId", financeId));
		}
		if (recieveNoticeId != null) {
			criteria.add(Restrictions.eq("recieveNoticeId", recieveNoticeId));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		if (clerkId != null) {
			criteria.add(Restrictions.eq("clerkId", clerkId));
		}
		if (StringUtils.isNotBlank(tallyingNumber)) {
			criteria.add(Restrictions.like("tallyingNumber", "%" + tallyingNumber + "%"));
		}
		if (tallyingState != null && tallyingState.length > 0) {
			TallyingStateEnum[] tallyingStates = new TallyingStateEnum[tallyingState.length];
			boolean isNull = false;
			for (int i = 0; i < tallyingState.length; i++) {
				tallyingStates[i] = TallyingStateEnum.valueOf(tallyingState[i]);
				if (tallyingState[i] == null) {
					isNull = true;
				}
			}
			if (!isNull) {
				criteria.add(Restrictions.in("tallyingState", tallyingStates));
			}
		}
		if (sort != null && sort == true) {
			criteria.addOrder(Order.desc("createDateTime"));
		}
		criteria.addOrder(Order.asc("tallyingId"));
		return PersistUtil.get(criteria, page);
	}
	
	@Override
	public List<TallyingEntity> inquireTallyingInProcess(Long logisticsCompanyId, Set<TallyingStateEnum> tallyStates) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TallyingEntity.class);
		if (tallyStates.size() > 0) {
			criteria.add(Restrictions.in("tallyingState", tallyStates));
		}
		if (logisticsCompanyId != null) {
			criteria.add(Restrictions.eq("logisticsCompanyId", logisticsCompanyId));
		}
		return PersistUtil.get(criteria);
	}

	@Override
	public List<Long> countWaitSupervisionTallying() {
//		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceApplicationEntity.class);
		DetachedCriteria criteria = DetachedCriteria.forClass(ContainerInventoryEntity.class);
//		criteria.add(Restrictions.eq("financeState",FinanceStateEnum.WAIT_PROTOCOL));
		criteria.add(Restrictions.in("supervisionState", new SupervisionStateEnum[] { SupervisionStateEnum.ALREADY_REPORT, SupervisionStateEnum.DELIVERY_SURE }));
		criteria.setProjection(Projections.projectionList().add(Projections.groupProperty("financeId")));
		return PersistUtil.get(criteria);
	}

	@Override
	public List<TallyingEntity> inquireTallyingList(Long recieveNoticeId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(TallyingEntity.class);
		criteria.add(Restrictions.eq("recieveNoticeId", recieveNoticeId));
		return PersistUtil.get(criteria);
	}
}
